package com.test.mytest.awt;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class RobotUtils {
	private static String defaultKey = "huangjianxiong";

	public static void main(String[] args) throws AWTException {
		String a = test();
		Robot rb = getRobot();
		keyPressString(a, rb);
	}
	/**
	 * @param a
	 * @param rb
	 */
	public static void keyPressString(String a, Robot rb) {
		char[] charArray = a.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			keyPressChar(rb, charArray[i]);
		}
	}
	public static Robot getRobot(){
		try {
			Robot rb = new Robot();
			return rb;
		} catch (AWTException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param rb
	 * @param charAt
	 */
	public static void keyPressChar(Robot rb, char charAt) {
		int keyCode = (int) charAt;
		keyPressKeyCode(rb, keyCode);
	}

	/**
	 * @param rb
	 * @param keyCode
	 */
	public static void keyPressKeyCode(Robot rb, int keyCode) {
		if (keyCode >= 65 && keyCode <= 90) {
			shiftKey(rb, keyCode);
		} else {
			if (keyCode >= 97 && keyCode <= 122) {
				keyCode = keyCode - 32;
			}
			rb.keyPress(keyCode);
			rb.keyRelease(keyCode);
		}
	}

	/**
	 * @param rb
	 * @param charAt
	 */
	public static void shiftKey(Robot rb, int charAt) {
		rb.keyPress(KeyEvent.VK_SHIFT);
		rb.keyPress(charAt);
		rb.keyRelease(charAt);
		rb.keyRelease(KeyEvent.VK_SHIFT);
	}

	/**
	 * 
	 */
	public static String test() {
		String s = "huangjianxiong";
		String stringXor = StringXor(s, "黄建雄");
		String encod = encod(stringXor);
		String decod = decod(encod);
		String stringXor2 = StringXor(decod, "黄建雄");
		System.out.println(encod);
		System.out.println("加码==" + encod + "--解码==" + decod + "==" + stringXor2);
		return encod;
	}

	/**
	 * 家码
	 * 
	 * @param s
	 * @return
	 */
	public static String encod(String s) {
		try {
			String ss = Base64.getEncoder().encodeToString(s.getBytes("utf-8"));
			ss = StringXor(ss, defaultKey);
			String sss = Base64.getEncoder().encodeToString(ss.getBytes("utf-8"));
			return sss;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 解码
	 * 
	 * @param s
	 * @return
	 */
	public static String decod(String s) {
		try {
			byte[] decode2 = Base64.getDecoder().decode(s);
			String ss = new String(decode2, "utf-8");
			ss = StringXor(ss, defaultKey);
			byte[] decode = Base64.getDecoder().decode(ss);
			String string = new String(decode, "utf-8");
			return string;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static String StringXor(String Str, String key) {
		char[] StrChars = Str.toCharArray();
	    char[] KeyChars = key.toCharArray();
	    for (int i = 0; i < Str.length(); i++){
	    	for(int j = 0; j < key.length(); j++){
	    		StrChars[i] ^= KeyChars[j++ % key.length()];
	    		StrChars[i] ^=(j+i)^j^i;
	    	}
	    }
	    return new String(StrChars);
	}
}
